package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.bean.TimeLineRecord;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.MeasureUtil;
import com.smalls.newvideotwo.view.ItemSpecKeyPress;
import com.smalls.newvideotwo.view.MyGridLayoutManager;
import com.smalls.newvideotwo.view.MyItemClick;

import java.util.ArrayList;
import java.util.List;

import static com.smalls.newvideotwo.utils.Tools.setRelativeLayoutParam;

/**
 * 项目名称：NewVideoFour
 * 类描述：RecyclerView嵌套RecyclerView，该为最外层的adapter，也就是行（eg：row=title+RecyclerView）
 * 创建人：Sophia Huang
 * 创建时间：2018/4/26 22:40
 * 修改人：Sophia Huang
 * 修改时间：2018/4/26 22:40
 * 修改备注：
 */
public class RowAdapter extends RecyclerView.Adapter {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + RowAdapter.class.getSimpleName():RowAdapter.class.getSimpleName();

    private Context mContext;
    private List<TimeLineRecord> mRowList = null;
    private View.OnFocusChangeListener mOnFocusListener = null;
    private View.OnClickListener mOnClickListener=null;
    private ItemSpecKeyPress mDownKeyPress = null;
    private ItemSpecKeyPress mUpKeyPress = null;
    private MyItemClick mItemClick=null;
    public RowAdapter(Context context, List<TimeLineRecord> rowList, ItemSpecKeyPress upKeyPress, ItemSpecKeyPress downKeyPress, MyItemClick myItemClick) {
        mContext = context;
        mRowList = rowList;
        this.mDownKeyPress = downKeyPress;
        this.mUpKeyPress = upKeyPress;
        this.mItemClick = myItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RowHolder(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final RowHolder rowHolder = (RowHolder) holder;
        Log.i(TAG,"onBindViewHolder position="+position);
        TimeLineRecord item = null;
        if (mRowList != null && mRowList.size() > position) {
            item = mRowList.get(position);
        }
        rowHolder.itemView.setTag(position);
        TimeLineRecord bean = (TimeLineRecord) this.mRowList.get(position);
        if (bean.getTimeTv() != null) {
            rowHolder.info_timerange_tv.setText(bean.getTimeTv());
        }
        if (bean.getRecords() != null) {
            rowHolder.info_num_tv.setText(mContext.getString(R.string.total_movelist_number)
                    + bean.getRecords().size()
                    + mContext.getString(R.string.total_movelist_numset));
        }

        NewHistoryAdapter mediaAdapter = new NewHistoryAdapter(position,item.getRecords(),mUpKeyPress,mDownKeyPress,mItemClick);
        rowHolder.mAdapter = mediaAdapter;
        if (mOnFocusListener != null) {
            mediaAdapter.setOnFocusListener(mOnFocusListener);
        }
        if(mOnClickListener!=null) {
            mediaAdapter.setOnClickListenter(mOnClickListener);
        }
        rowHolder.gv_albums.setAdapter(mediaAdapter);
        if(item.getRecords()!=null) {
            int line = item.getRecords().size() % 5 == 0 ? item.getRecords().size() / 5 : item.getRecords().size() / 5 + 1;
            RelativeLayout.LayoutParams imageSwitcherPa = (RelativeLayout.LayoutParams) rowHolder.recyclerViewRe
                    .getLayoutParams();
            //imageSwitcherPa.width = (int) MeasureUtil.getWidthSize(182);
            imageSwitcherPa.height = (int) MeasureUtil.getHeightSize(line * 436+10);
            //imageSwitcherPa.topMargin = (int) MeasureUtil.getHeightSize(36);
            imageSwitcherPa.leftMargin = (int) MeasureUtil.getWidthSize(224);
            imageSwitcherPa.rightMargin = (int) MeasureUtil.getWidthSize(104);
            rowHolder.recyclerViewRe.setLayoutParams(imageSwitcherPa);
        }
        if (mOnFocusListener != null) {
            holder.itemView.setOnFocusChangeListener(mOnFocusListener);
        }
    }

    @Override
    public int getItemCount() {
        if (null != mRowList) {
            return mRowList.size();
        }
        return 0;
    }

    public void setOnFocusListener(View.OnFocusChangeListener onFocusListener) {
        mOnFocusListener = onFocusListener;
    }

    public void setmOnClickListener(View.OnClickListener listener) {
        mOnClickListener = listener;
    }

    private class RowHolder extends RecyclerView.ViewHolder {

        RecyclerView gv_albums;
        TextView info_num_tv;
        TextView info_timerange_tv;
        LinearLayout llear_layout;
        RelativeLayout recyclerViewRe;
        NewHistoryAdapter mAdapter;

        public RowHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_listview, parent, false));
            info_timerange_tv = (TextView) itemView.findViewById(R.id.new_info_timerange_tv);
            info_num_tv = (TextView) itemView.findViewById(R.id.new_info_num_tv);
            llear_layout = (LinearLayout) itemView.findViewById(R.id.new_llear_layout);
            gv_albums = (RecyclerView) itemView.findViewById(R.id.new_gv_albums);
            recyclerViewRe = (RelativeLayout) itemView.findViewById(R.id.new_recycler_view_re);
            GridLayoutManager gridLayoutManager = new MyGridLayoutManager(mContext,5);
            gv_albums.setLayoutManager(gridLayoutManager);

            if(mRowList!=null) {
                recyclerViewRe = (RelativeLayout) itemView.findViewById(R.id.new_recycler_view_re);
            }
            mAdapter = null;
        }
    }

}
